<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>
<body>
    <input  type="text" placeholder="请输入一个数字" id="n1">
    <br>
    <input  type="text" placeholder="请输入另一个数字" id="n2">
    <br>
    <button onclick="calc('+')">加</button>
    <button onclick="calc('-')">减</button>
    <button onclick="calc('*')">乘</button>
    <button onclick="calc('/')">除</button>
    <h4>结果：<span id="result"></span></h4>
    <script>
        function calc(o) {
            //获取用户输入的元素,这里获取的是字符串类型，所以需要转换成数字类型
            let n1 = document.getElementById('n1').value;
            let n2 = document.getElementById('n2').value;
            let result = 0;
            switch (o) {
                case '+':
                    result = parseInt(n1) + parseInt(n2);
                    break;
                case '-':
                    result = parseInt(n1) - parseInt(n2);
                    break;
                case '*':
                    result = parseInt(n1) * parseInt(n2)
                    break;
                case '/':
                    if (parseInt(n2) === 0) {
                        alert('除数不能为0');
                        return;
                    }
                    result = parseInt(n1) / parseInt(n2)
                    break;
            }
            document.getElementById('result').innerHTML = result;
        }
    </script>
</body>
</html>